<?php
class Region {
	public function __construct() {
		
	}
	
	public function get_regions_by_country_id($country_id) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT region_id, region_name FROM regions WHERE country_id = '$country_id' ORDER BY region_name";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					//change this to set whatever
					//$option_list[] = "<option value='$region_id'>$region_name</option>";
					$each = array(
						"id" => "$region_id", 
						"name" => "$region_name",
					);
					$option_list[] = $each;
				}
				return $option_list;
			}
			$result->close();
		}
		$mysqli->close();
	}
	
	public function get_region_list($country_id) {
		include("inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php");
		$mysqli = new mysqli($host, $user, $password, $database);
		if(mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT region_id, region_name FROM regions WHERE country_id = '$country_id' ORDER BY region_name";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
					extract($row);
					$option_list[] = "<option value='$region_id'>$region_name</option>";
				}
				return $option_list;
			}
			$result->close();
		}
		$mysqli->close();
	}
	
	public function delete_region_as_visited($user_id, $region_id) {
		include "inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php";
		$mysqli = new mysqli($host, $user, $password, $database);
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "DELETE FROM user_region WHERE region_id = '$region_id' AND user_id = '$user_id'";
		$mysqli->query($sql);
		$mysqli->close();
	}
	
	public function add_region_as_visited($region_id, $user_id) {
		include "inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php";
		$mysqli = new mysqli($host, $user, $password, $database);
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT user_region FROM user_region WHERE user_id = '$user_id' AND region_id = '$region_id'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				return 2;
			} else {
				$sql = "INSERT INTO user_region(user_id, region_id) VALUES('$user_id', '$region_id')";
				$mysqli->query($sql);
			}
		}
	}
	
	public function add_region($region, $country, $lat, $long) {
		include "inc_/sup3rs3cr3tt3xt4m3t0kn0vv.php";
		$mysqli = new mysqli($host, $user, $password, $database);
		if (mysqli_connect_errno()) {
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		$sql = "SELECT region_id FROM regions WHERE region_name = '$region'";
		if($result = $mysqli->query($sql)) {
			if($result->num_rows >= 1) {
				return 0;
			} else {
				$sql = "INSERT INTO regions(country_id, region_name, region_lat, region_long) VALUES('$country', '$region', '$lat', '$long')";
				$mysqli->query($sql);

				return 2;
			}
		}		
	}
}
?>
